import json
import sys
sys.path.append("../")
from services.MenuService import *
from controller.BaseController import *
import hashlib

class MenuController(BaseController):
    def __init__(self,args):
        super(MenuController,self).__init__()
        self.args = args
        self.service = MenuService()

    def getMenuStr(self):
        newt = self.service.getMenuObj()
        return json.dumps(newt)

    def getMenuList(self):
        return self.service.getMenuList()

    def menu_gets(self, roleid, type):
        if type == "front":
            sp = self.service.getFontMenuObj()
        else:
            sp = self.service.getMenuObj()
        menuRs = self.service.getRols(roleid)
        if len(menuRs) > 0:
            idsArr = menuRs[0]["ids"].replace(" ","")[1:-1].split(",")
        else:
            idsArr = []
        newst = sp
        childst = []
        for t in sp["children"]:
            if str(t["id"]) in idsArr:
                tempst = t
                childst1 = []
                if "children" in t and len(t["children"]) > 0:
                    for t1 in t["children"]:
                        if str(t1["id"]) in idsArr:
                            childst1.append(t1)
                tempst["children"] = childst1
                childst.append(tempst)
        newst["children"] = childst
        return newst



